/**
 * 2
 *  
 * @Author : Ruoyu Wang
 * @DateTime : 24-9-27 下午6:22
 */

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main() {
    long long n, t;
    cin >> n;
    vector<int> nums(n);
    for (int i = 0; i < n; ++i) {
        cin >> nums[i];
    }
    cin >> t;

    sort(nums.begin(), nums.end());
    long long res = 0;
    int diff = 0x3f3f3f3f;

    for (int i = 0; i < nums.size() - 2; ++i) {
        int l = i + 1, r = nums.size() - 1;
        while (l < r) {
            long long sum = nums[i] + nums[l] + nums[r];
            if (sum == t) {
                res = sum;
                break;
            }
            if (abs(t - sum) < diff) {
                diff = abs(t - sum);
                res = sum;
            }
            if (sum < t) {
                ++l;
            } else {
                --r;
            }
        }
        if (res == t) {
            break;
        }
    }

    cout << res << endl;

    return 0;
}